package com.ruikong.dazhi.controller.base;

import com.github.pagehelper.PageInfo;
import com.ruikong.dazhi.controller.result.BaseResult;
import com.ruikong.dazhi.controller.result.ResultApi;
import com.ruikong.dazhi.entity.base.Role;
import com.ruikong.dazhi.model.input.BasePageInput;
import com.ruikong.dazhi.model.input.RoleInput;
import com.ruikong.dazhi.service.base.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/data")
@EnableAutoConfiguration
@Api(tags = "角色管理")
public class RoleController {

    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private IRoleService roleService;

    /**
     * 新增
     * @param roleInput
     * @return
     */
    @ApiOperation(value = "新增")
    @RequestMapping(value = "/addRole", method = RequestMethod.POST)
    public BaseResult addRole(@RequestBody RoleInput roleInput) {
        BaseResult result = new BaseResult();
        try {
            int effect = roleService.addRole(result, roleInput);
            if(effect > 0){
                result.setCode(ResultApi.successCode);
            } else {
                result.setCode(ResultApi.failCode);
            }
        } catch (Exception e) {
            logger.error("addRole", e.getMessage());
            e.printStackTrace();
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.addFailMsg);
        }
        return result;
    }


    /**
     * 获取列表
     * @param input
     * @return
     */
    @ApiOperation(value = "获取列表_分页")
    @RequestMapping(value = "/getRoleListPage", method = RequestMethod.POST)
    public PageInfo<Role> getRoleListPage(@RequestBody BasePageInput input) {
        PageInfo<Role> pageInfo = new PageInfo<>();
        try {
            pageInfo = roleService.getRoleListPage(input);
        } catch (Exception e) {
            logger.error("getRoleListPage", e.getMessage());
        }
        return pageInfo;
    }

    /**
     * 获取列表
     *
     * @return
     */
    @ApiOperation(value = "获取列表")
    @RequestMapping(value = "/getRoleList", method = RequestMethod.POST)
    public BaseResult getRoleList() {

        BaseResult result = new BaseResult();
        try {
            List<Role> roles = roleService.getRoleList();
            result.setCode(ResultApi.successCode);
            result.setMsg(ResultApi.getSuccessMsg);
            result.setData(roles);

        } catch (Exception e) {
            logger.error("getRoleList", e.getMessage());
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.getFailMsg);
        }
        return result;
    }

    /**
     * 编辑
     * @param roleInput
     * @return
     */
    @ApiOperation(value = "编辑")
    @RequestMapping(value = "/updateRole", method = RequestMethod.POST)
    public BaseResult updateRole(@RequestBody RoleInput roleInput) {
        BaseResult result = new BaseResult();
        try {
            int effect = roleService.updateRole(roleInput, result);
            if(effect > 0){
                result.setCode(ResultApi.successCode);
                result.setMsg(ResultApi.updateSuccessMsg);
            } else {
                result.setCode(ResultApi.failCode);
                result.setMsg(ResultApi.updateFailMsg);
            }
        } catch (Exception e) {
            logger.error("updateRole", e.getMessage());
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.updateFailMsg);
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 删除
     * @param role
     * @return
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/deleteRole", method = RequestMethod.POST)
    public BaseResult deleteRole(@RequestBody Role role) {

        BaseResult result = new BaseResult();
        try {
           int effect = roleService.deleteRole(role, result);
           if(effect > 0){
               result.setCode(ResultApi.successCode);
               result.setMsg(ResultApi.deleteSuccessMsg);
           } else {
               result.setCode(ResultApi.failCode);
               result.setMsg(ResultApi.deleteFailMsg);
           }
        } catch (Exception e) {
            logger.error("deleteRole", e.getMessage());
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.deleteFailMsg);
        }
        return result;
    }

}
